package com.wy.websocket.netty;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpServerCodec;
import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler;
import io.netty.handler.stream.ChunkedWriteHandler;
import io.netty.handler.timeout.IdleStateHandler;

/**
 * Netty支持WebSocket
 *
 * @author 飞花梦影
 * @date 2024-04-30 10:28:01
 * @git {@link https://github.com/dreamFlyingFlower}
 */
public class WebSocketServerInitializer extends ChannelInitializer<SocketChannel> {

	/**
	 * 初始化通道以及配置对应管道的处理器
	 * 
	 * @param channel
	 * @throws Exception
	 */
	@Override
	protected void initChannel(SocketChannel channel) throws Exception {

		ChannelPipeline pipeline = channel.pipeline();
		pipeline.addLast(new HttpServerCodec());

		pipeline.addLast(new ChunkedWriteHandler());
		pipeline.addLast(new HttpObjectAggregator(1024 * 64));

		// 针对客户端,如果在1分钟时间内没有向服务端发送读写心跳(ALL),则主动断开连接 如果有读空闲和写空闲,则不做任何处理
		pipeline.addLast(new IdleStateHandler(8, 10, 12));
		// 自定义的空闲状态检测的handler
		pipeline.addLast(new HeartBeatHandler());

		pipeline.addLast(new WebSocketServerProtocolHandler("/ws"));
		// 自定义的handler
		pipeline.addLast(new WebSocketServerListenerHandler());
	}
}